<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>xmake新增对WDK驱动编译环境支持</title>
    <meta name="description" content="xmake v2.2.1新版本现已支持WDK驱动编译环境，我们可以直接在系统原生cmd终端下，执行xmake进行驱动编译，甚至配合vscode, sublime text, IDEA等编辑器+xmake插件去开发WDK驱动。下面是xmake支持的一些编辑器插件，用户可以挑选自己喜欢的编辑器配合xmake来使用： ...">

    
    <meta name="keywords" content="xmake,lua,WDK,kmdf,umdf,wdm,driver,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/cn/2018/06/14/support-wdk/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />

    <!-- css -->
    <link href="/css/reward.css" rel="stylesheet" type="text/css"> 




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/cn" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/?lang=0">
                    
                        <i class="fa fa-home"></i>English
                    </a>
                </li>

                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>项目
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/archive/">
                            
                        
                            <i class="fa fa-archive"></i>归档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/category/">
                            
                        
                            <i class="fa fa-th-list"></i>分类
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/tag/">
                            
                        
                            <i class="fa fa-tags"></i>标记
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/docs/">
                            
                        
                            <i class="fa fa-book"></i>文档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="https://xmake.io/#/zh-cn/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>社区
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/donation/">
                            
                        
                            <i class="fa fa-heart"></i>捐助
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/about/">
                            
                        
                            <i class="fa fa-user"></i>关于
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>xmake新增对WDK驱动编译环境支持</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2018-06-14
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#xmake" title="Category: xmake" rel="category">xmake</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a-->
        <a href="/cn/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a>&nbsp;
    
        <!--a href="/tag/#lua" title="Tag: lua" rel="tag">lua</a-->
        <a href="/cn/tag/#lua" title="Tag: lua" rel="tag">lua</a>&nbsp;
    
        <!--a href="/tag/#WDK" title="Tag: WDK" rel="tag">WDK</a-->
        <a href="/cn/tag/#WDK" title="Tag: WDK" rel="tag">WDK</a>&nbsp;
    
        <!--a href="/tag/#kmdf" title="Tag: kmdf" rel="tag">kmdf</a-->
        <a href="/cn/tag/#kmdf" title="Tag: kmdf" rel="tag">kmdf</a>&nbsp;
    
        <!--a href="/tag/#umdf" title="Tag: umdf" rel="tag">umdf</a-->
        <a href="/cn/tag/#umdf" title="Tag: umdf" rel="tag">umdf</a>&nbsp;
    
        <!--a href="/tag/#wdm" title="Tag: wdm" rel="tag">wdm</a-->
        <a href="/cn/tag/#wdm" title="Tag: wdm" rel="tag">wdm</a>&nbsp;
    
        <!--a href="/tag/#driver" title="Tag: driver" rel="tag">driver</a-->
        <a href="/cn/tag/#driver" title="Tag: driver" rel="tag">driver</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <p><a href="https://github.com/xmake-io/xmake">xmake</a> v2.2.1新版本现已支持WDK驱动编译环境，我们可以直接在系统原生cmd终端下，执行xmake进行驱动编译，甚至配合vscode, sublime text, IDEA等编辑器+xmake插件去开发WDK驱动。</p>

<p>下面是xmake支持的一些编辑器插件，用户可以挑选自己喜欢的编辑器配合xmake来使用：</p>

<ul>
  <li><a href="https://github.com/xmake-io/xmake-idea">xmake-idea</a></li>
  <li><a href="https://github.com/xmake-io/xmake-vscode">xmake-vscode</a></li>
  <li><a href="https://github.com/xmake-io/xmake-sublime">xmake-sublime</a></li>
</ul>

<h2 id="wdk环境介绍">WDK环境介绍</h2>

<p>首先，我们先简单介绍下WDK10的编译环境的安装方式，我们可以看下微软的官方文档：<a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/download-the-wdk">Download the Windows Driver Kit (WDK)</a></p>

<p>里面介绍了两种环境：</p>

<ol>
  <li>下载WDK开发包，直接安装到系统并集成到VS的开发环境中</li>
  <li>下载EWDK iso镜像（内含完整WDK开发环境），直接挂载后，运行LaunchBuildEnv进入cmd环境</li>
</ol>

<p>xmake对于这两种环境都是完全支持的，如果用户直接下载安装WDK环境到本地系统，那么不需要任何配置，只需要执行：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake
</code></pre>
</div>

<p>xmake会自动检测到WDK的安装环境，然后编译相关驱动项目，如果用户是直接挂载的EWDK iso开发镜像，那么编译前配置下WDK所在路径即可：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake f --wdk<span class="o">=</span><span class="s2">"G:</span><span class="se">\P</span><span class="s2">rogram Files</span><span class="se">\W</span><span class="s2">indows Kits</span><span class="se">\1</span><span class="s2">0"</span> 
<span class="gp">$ </span>xmake
</code></pre>
</div>

<p>更多详情可以参考：<a href="https://github.com/xmake-io/xmake/issues/159">#159</a></p>

<h2 id="wdk驱动实例">WDK驱动实例</h2>

<p>xmake支持umdf, kmdf, wdm驱动项目的维护，也是采用一系列扩展的WDK rule规则来实现，类似：<a href="http://tboox.org/cn/2018/05/30/support-qt/">Qt编译环境的支持</a>。</p>

<p>目前支持的规则有如下这些：</p>

<ul>
  <li>rule(“wdk.driver”)</li>
  <li>rule(“wdk.binary”)</li>
  <li>rule(“wdk.static”)</li>
  <li>
    <p>rule(“wdk.shared”)</p>
  </li>
  <li>rule(“wdk.env.kmdf”)</li>
  <li>rule(“wdk.env.umdf”)</li>
  <li>rule(“wdk.env.wdm”)</li>
</ul>

<p>其中，<code class="highlighter-rouge">wdk.env.*</code>规则描述驱动编译的环境，<code class="highlighter-rouge">wdk.driver</code>, <code class="highlighter-rouge">wdk.static</code>描述编译的目标类型，两者可以互相结合使用，我们既可以用来编译驱动程序，也可以用来编译基于wdk环境的静态库、可执行程序。</p>

<p>下面，通过一些例子可以简单看下使用方式，具体例子代码见<a href="https://github.com/xmake-io/xmake/tree/master/tests/projects/wdk/">wdk-examples</a>，其中的项目代码是从<a href="https://github.com/Microsoft/Windows-driver-samples">Windows-driver-samples</a>移植过来的。</p>

<h4 id="umdf驱动程序">umdf驱动程序</h4>

<p>我们通过同时应用<code class="highlighter-rouge">wdk.driver</code>, <code class="highlighter-rouge">wdk.env.umdf</code>规则，来描述这个target作为umdf驱动程序来编译：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"echo"</span><span class="p">)</span>
    <span class="n">add_rules</span><span class="p">(</span><span class="s2">"wdk.driver"</span><span class="p">,</span> <span class="s2">"wdk.env.umdf"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"driver/*.c"</span><span class="p">)</span> 
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"driver/*.inx"</span><span class="p">)</span>
    <span class="n">add_includedirs</span><span class="p">(</span><span class="s2">"exe"</span><span class="p">)</span>
</code></pre>
</div>

<p>我们也可以通过<code class="highlighter-rouge">wdk.binary</code>, <code class="highlighter-rouge">wdk.env.umdf</code>规则，来描述一个基于wdk/umdf编译环境的上层可执行程序：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"app"</span><span class="p">)</span>
    <span class="n">add_rules</span><span class="p">(</span><span class="s2">"wdk.binary"</span><span class="p">,</span> <span class="s2">"wdk.env.umdf"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"exe/*.cpp"</span><span class="p">)</span> 
</code></pre>
</div>

<h4 id="kmdf驱动程序">kmdf驱动程序</h4>

<p>kmdf的项目描述跟刚才的umdf类似，只需要把<code class="highlighter-rouge">wdk.env.umdf</code>换成<code class="highlighter-rouge">wdk.env.kmdf</code>的环境规则就行了。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"nonpnp"</span><span class="p">)</span>
    <span class="n">add_rules</span><span class="p">(</span><span class="s2">"wdk.driver"</span><span class="p">,</span> <span class="s2">"wdk.env.kmdf"</span><span class="p">)</span>
    <span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.tracewpp.flags"</span><span class="p">,</span> <span class="s2">"-func:TraceEvents(LEVEL,FLAGS,MSG,...)"</span><span class="p">)</span>
    <span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.tracewpp.flags"</span><span class="p">,</span> <span class="s2">"-func:Hexdump((LEVEL,FLAGS,MSG,...))"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"driver/*.c"</span><span class="p">,</span> <span class="p">{</span><span class="n">rule</span> <span class="o">=</span> <span class="s2">"wdk.tracewpp"</span><span class="p">})</span> 
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"driver/*.rc"</span><span class="p">)</span>

<span class="n">target</span><span class="p">(</span><span class="s2">"app"</span><span class="p">)</span>
    <span class="n">add_rules</span><span class="p">(</span><span class="s2">"wdk.binary"</span><span class="p">,</span> <span class="s2">"wdk.env.kmdf"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"exe/*.c"</span><span class="p">)</span> 
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"exe/*.inf"</span><span class="p">)</span>
</code></pre>
</div>

<p>这个项目里面，需要特别注意的是，我们还用到了tracewpp对一些源文件的预处理，对于tracewpp任务的介绍，可以看下官方文档<a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/tracewpp-task">tracewpp-task</a>，这里就不多做说明了。</p>

<p>我们直接说下，如何在xmake的项目里应用tracewpp规则吧，由于这个规则并不是对当前target所有源文件都去处理的，因此我们只对需要的源文件进行应用这个规则，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_files</span><span class="p">(</span><span class="s2">"driver/*.c"</span><span class="p">,</span> <span class="p">{</span><span class="n">rule</span> <span class="o">=</span> <span class="s2">"wdk.tracewpp"</span><span class="p">})</span> 
<span class="n">add_files</span><span class="p">(</span><span class="s2">"driver/dir/test.c"</span><span class="p">,</span> <span class="p">{</span><span class="n">rule</span> <span class="o">=</span> <span class="s2">"wdk.tracewpp"</span><span class="p">})</span> 
</code></pre>
</div>

<p>当然tracewpp还会有一些自己的特殊选项，用户有时候需要自己根据需要来设置，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.tracewpp.flags"</span><span class="p">,</span> <span class="s2">"-func:TraceEvents(LEVEL,FLAGS,MSG,...)"</span><span class="p">)</span>
<span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.tracewpp.flags"</span><span class="p">,</span> <span class="s2">"-func:Hexdump((LEVEL,FLAGS,MSG,...))"</span><span class="p">)</span>
</code></pre>
</div>

<p>关于<code class="highlighter-rouge">add_values</code>的使用说明，可以看下文档：<a href="https://xmake.io/#/zh/manual?id=targetset_values">add_values和set_values的使用说明</a>，简单来说，就是用来给对应规则传递扩展参数设置的。</p>

<h4 id="wdm驱动程序">wdm驱动程序</h4>

<p>wdm的项目描述也跟umdf类似，只需要把<code class="highlighter-rouge">wdk.env.umdf</code>换成<code class="highlighter-rouge">wdk.env.wdm</code>的环境规则就行了。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"kcs"</span><span class="p">)</span>
    <span class="n">add_rules</span><span class="p">(</span><span class="s2">"wdk.driver"</span><span class="p">,</span> <span class="s2">"wdk.env.wdm"</span><span class="p">)</span>
    <span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.man.flags"</span><span class="p">,</span> <span class="s2">"-prefix Kcs"</span><span class="p">)</span>
    <span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.man.resource"</span><span class="p">,</span> <span class="s2">"kcsCounters.rc"</span><span class="p">)</span>
    <span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.man.header"</span><span class="p">,</span> <span class="s2">"kcsCounters.h"</span><span class="p">)</span>
    <span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.man.counter_header"</span><span class="p">,</span> <span class="s2">"kcsCounters_counters.h"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"*.c"</span><span class="p">,</span> <span class="s2">"*.rc"</span><span class="p">,</span> <span class="s2">"*.man"</span><span class="p">)</span> 
</code></pre>
</div>

<p>上述代码，还添加了一些.man文件用来预处理一些manifest，具体相关的任务藐视，可以参考官方文档说明：<a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/ctrpp-task">ctrpp-task</a>。
里面也有相关的一些特殊配置选项，目前xmake支持的配置有：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.man.flags"</span><span class="p">,</span> <span class="s2">"-prefix Kcs"</span><span class="p">)</span>
<span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.man.prefix"</span><span class="p">,</span> <span class="s2">"Kcs"</span><span class="p">)</span>
<span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.man.resource"</span><span class="p">,</span> <span class="s2">"kcsCounters.rc"</span><span class="p">)</span>
<span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.man.header"</span><span class="p">,</span> <span class="s2">"kcsCounters.h"</span><span class="p">)</span>
<span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.man.counter_header"</span><span class="p">,</span> <span class="s2">"kcsCounters_counters.h"</span><span class="p">)</span>
</code></pre>
</div>

<p>下面再贴个wdm驱动的例子，这个例子中，除了之前讲的tracewpp，我们还加了.mof的文件处理，对于.mof文件，xmake会自动应用内置的<code class="highlighter-rouge">wdk.mof</code>规则，详细说明见：<a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/mofcomp-task">mofcomp-task</a></p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"msdsm"</span><span class="p">)</span>
    <span class="n">add_rules</span><span class="p">(</span><span class="s2">"wdk.driver"</span><span class="p">,</span> <span class="s2">"wdk.env.wdm"</span><span class="p">)</span>
    <span class="n">add_values</span><span class="p">(</span><span class="s2">"wdk.tracewpp.flags"</span><span class="p">,</span> <span class="s2">"-func:TracePrint((LEVEL,FLAGS,MSG,...))"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"*.c"</span><span class="p">,</span> <span class="p">{</span><span class="n">rule</span> <span class="o">=</span> <span class="s2">"wdk.tracewpp"</span><span class="p">})</span> 
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"*.rc"</span><span class="p">,</span> <span class="s2">"*.inf"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"*.mof|msdsm.mof"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"msdsm.mof"</span><span class="p">,</span> <span class="p">{</span><span class="n">values</span> <span class="o">=</span> <span class="p">{</span><span class="n">wdk_mof_header</span> <span class="o">=</span> <span class="s2">"msdsmwmi.h"</span><span class="p">}})</span> 
</code></pre>
</div>

<p>对于.mof的配置选项，有些配置并不是全局应用于target的，对每个文件需要单独配置，这个时候，就不能直接使用<code class="highlighter-rouge">set_values</code>和<code class="highlighter-rouge">add_values</code>了，需要在<code class="highlighter-rouge">add_files</code>中设置相关values。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_files</span><span class="p">(</span><span class="s2">"msdsm.mof"</span><span class="p">,</span> <span class="p">{</span><span class="n">values</span> <span class="o">=</span> <span class="p">{</span><span class="n">wdk_mof_header</span> <span class="o">=</span> <span class="s2">"msdsmwmi.h"</span><span class="p">}})</span> 
<span class="n">add_files</span><span class="p">(</span><span class="s2">"msdsm.mof"</span><span class="p">,</span> <span class="p">{</span><span class="n">values</span> <span class="o">=</span> <span class="p">{[</span><span class="s2">"wdk.mof.header"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"msdsmwmi.h"</span><span class="p">}})</span> 
</code></pre>
</div>

<p>上面两种设置方式都是有效的，由于受限于lua的语法，为了考虑可读性，xmake通过_下划线来简化key的设置，这个设置相当于单独对msdsm.mof文件设置了<code class="highlighter-rouge">set_values("wdk.mof.header", "msdsmwmi.h")</code>。</p>

<h2 id="生成驱动包">生成驱动包</h2>

<p>如果平常开发调试通过后，我们也可以通过以下命令生成.cab驱动包来发布驱动程序：</p>

<div class="language-console highlighter-rouge"><pre class="highlight"><code><span class="w">$ </span><span class="nc">xmake</span><span class="kv"> [p|package]
</span><span class="w">$ </span><span class="nc">xmake</span><span class="kv"> [p|package] -o outputdir
</span></code></pre>
</div>

<p>输出的目录结构如下：</p>

<div class="highlighter-rouge"><pre class="highlight"><code>  - drivers
    - sampledsm
       - debug/x86/sampledsm.cab
       - release/x64/sampledsm.cab
       - debug/x86/sampledsm.cab
       - release/x64/sampledsm.cab
</code></pre>
</div>

<h2 id="驱动签名">驱动签名</h2>

<p>默认编译我们是禁用签名的，如果想要在编译的同时，启用签名，可以通过<code class="highlighter-rouge">set_values("wdk.sign.mode", ...)</code>设置签名模式来启用。
只要启用了签名，那么平常的驱动构建、包括打包生成的.cab文件，都会自动对其进行签名。</p>

<h4 id="测试签名">测试签名</h4>

<p>测试签名一般本机调试时候用，可以使用xmake自带的test证书来进行签名，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"msdsm"</span><span class="p">)</span>
    <span class="n">add_rules</span><span class="p">(</span><span class="s2">"wdk.driver"</span><span class="p">,</span> <span class="s2">"wdk.env.wdm"</span><span class="p">)</span>
    <span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.sign.mode"</span><span class="p">,</span> <span class="s2">"test"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
</code></pre>
</div>

<p>不过这种情况下，需要用户手动在管理员模式下，执行一遍：<code class="highlighter-rouge">$xmake l utils.wdk.testcert install</code>，来生成和注册test证书到本机环境。
这个只需要执行一次就行了，后续就可以正常编译和签名了。</p>

<p>当然也可以使用本机已有的有效证书去签名，例如直接从sha1来选择合适的证书进行签名：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"msdsm"</span><span class="p">)</span>
    <span class="n">add_rules</span><span class="p">(</span><span class="s2">"wdk.driver"</span><span class="p">,</span> <span class="s2">"wdk.env.wdm"</span><span class="p">)</span>
    <span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.sign.mode"</span><span class="p">,</span> <span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.sign.thumbprint"</span><span class="p">,</span> <span class="s2">"032122545DCAA6167B1ADBE5F7FDF07AE2234AAA"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
</code></pre>
</div>

<p>或者从store/company来选择合适的证书进行签名：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"msdsm"</span><span class="p">)</span>
    <span class="n">add_rules</span><span class="p">(</span><span class="s2">"wdk.driver"</span><span class="p">,</span> <span class="s2">"wdk.env.wdm"</span><span class="p">)</span>
    <span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.sign.mode"</span><span class="p">,</span> <span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.sign.store"</span><span class="p">,</span> <span class="s2">"PrivateCertStore"</span><span class="p">)</span>
    <span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.sign.company"</span><span class="p">,</span> <span class="s2">"tboox.org(test)"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
</code></pre>
</div>

<h4 id="正式签名">正式签名</h4>

<p>对于正式签名，我们可以通过指定对应的正式签名证书文件进行签名：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"msdsm"</span><span class="p">)</span>
    <span class="n">add_rules</span><span class="p">(</span><span class="s2">"wdk.driver"</span><span class="p">,</span> <span class="s2">"wdk.env.wdm"</span><span class="p">)</span>
    <span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.sign.mode"</span><span class="p">,</span> <span class="s2">"release"</span><span class="p">)</span>
    <span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.sign.company"</span><span class="p">,</span> <span class="s2">"xxxx"</span><span class="p">)</span>
    <span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.sign.certfile"</span><span class="p">,</span> <span class="n">path</span><span class="p">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="p">.</span><span class="n">projectdir</span><span class="p">(),</span> <span class="s2">"xxxx.cer"</span><span class="p">))</span>
</code></pre>
</div>

<h2 id="生成低版本驱动">生成低版本驱动</h2>

<p>如果想在wdk10环境编译生成win7, win8等低版本系统支持的驱动，我们可以通过设置<code class="highlighter-rouge">wdk.env.winver</code>来切换系统版本：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.env.winver"</span><span class="p">,</span> <span class="s2">"win10"</span><span class="p">)</span>
<span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.env.winver"</span><span class="p">,</span> <span class="s2">"win10_rs3"</span><span class="p">)</span>
<span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.env.winver"</span><span class="p">,</span> <span class="s2">"win81"</span><span class="p">)</span>
<span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.env.winver"</span><span class="p">,</span> <span class="s2">"win8"</span><span class="p">)</span>
<span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.env.winver"</span><span class="p">,</span> <span class="s2">"win7"</span><span class="p">)</span>
<span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.env.winver"</span><span class="p">,</span> <span class="s2">"win7_sp1"</span><span class="p">)</span>
<span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.env.winver"</span><span class="p">,</span> <span class="s2">"win7_sp2"</span><span class="p">)</span>
<span class="n">set_values</span><span class="p">(</span><span class="s2">"wdk.env.winver"</span><span class="p">,</span> <span class="s2">"win7_sp3"</span><span class="p">)</span>
</code></pre>
</div>

<p>如果觉得每次修改xmake.lua去切换编译非常繁琐，我们也可以手动指定编译的目标程序支持的windows版本，来快速切换到对应的版本进行编译：</p>

<div class="language-console highlighter-rouge"><pre class="highlight"><code><span class="w">$ </span><span class="nc">xmake</span><span class="kv"> f --wdk_winver=[win10_rs3|win8|win7|win7_sp1]
</span><span class="w">$ </span><span class="nc">xmake</span><span class="kv">
</span></code></pre>
</div>

<p>目前支持的一些版本有：nt4, win2k, winxp, ws03, win6, vista, ws08, longhorn, win7, win8, win81, winblue, win10</p>

<p>然后通过_下划线，组合指定子版本：sp1, sp2, sp3, th2, rs1, rs2, rs3</p>

<p>xmake还提供了一些内置的版本值，在切换winver版本是，会自动改变，用于一些更加定制化的配置需求，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    
    <span class="n">on_load</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">)</span>
        <span class="kd">local</span> <span class="n">winnt_version</span> <span class="o">=</span> <span class="n">target</span><span class="p">:</span><span class="n">values</span><span class="p">(</span><span class="s2">"wdk.env.winnt_version"</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">winnt_version</span> <span class="o">&gt;</span> <span class="s2">"0x0A000000"</span> <span class="k">then</span>
            <span class="n">target</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="s2">"defines"</span><span class="p">,</span> <span class="s2">"TEST"</span><span class="p">)</span>
        <span class="k">end</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>上述代码通过判断WIN32_WINNT的版本值，来定制添加一些相关配置，这个版本值会根据<code class="highlighter-rouge">wdk.env.winver</code>的配置自动适配更新，目前提供的这些内置版本值还有：</p>

<div class="highlighter-rouge"><pre class="highlight"><code>target:values("wdk.env.winnt_version"): WIN32_WINNT
target:values("wdk.env.ntddi_version"): NTDDI_VERSION
target:values("wdk.env.winver_version"): WINVER
</code></pre>
</div>

<p>关于更多xmake下WDK开发相关介绍，请参考文档：<a href="https://xmake.io/#/zh/?id=wdk%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F">WDK驱动程序开发</a></p>

        </article>
        <hr>

        <!-- baidu ads -->
        

        <!-- reward -->
        <div style="text-align: center;">
            <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
              <span>赏</span>
            </button>
            <div id="QR" style="display: none;">
                <div id="wechat" style="display: inline-block">
                  <img id="wechat_qr" src="/static/img/weixin.png" alt="WeChat Pay"/>
                  <p>微信打赏</p>
                </div>
                <div id="alipay" style="display: inline-block">
                  <img id="alipay_qr" src="/static/img/alipay.png" alt="Alipay"/>
                  <p>支付宝打赏</p>
                </div>
            </div>
        </div>

        
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
        

        
        
            
        
            
            
            
                
                    
                        
                        <h2 id="similar_posts">相关文章</h2>
                        <ul>
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/17/xmake-update-v2.6.2/">xmake v2.6.2 发布，新增 Linux 内核驱动模块构建支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/03/xmake-update-v2.6.1/">xmake v2.6.1 发布，使用 Lua5.4 运行时，Rust 和 C++ 混合编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/30/xmake-update-v2.5.9/">xmake v2.5.9 发布，改进 C++20 模块，并支持 Nim, Keil MDK 和 Unity Build
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/08/xmake-update-v2.5.8/">xmake v2.5.8 发布，新增 Pascal/Swig 程序和 Lua53 运行时支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/08/29/xmake-update-v2.5.7/">xmake v2.5.7 发布，包依赖锁定和 Vala/Metal 语言编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            
            
        
        
            </ul>
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        <p><strong>上一篇</strong> <a href="/cn/2018/06/08/simplify-xmake-description/">xmake进阶之简化你的构建描述</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        <p><strong>下一篇</strong> <a href="/cn/2018/06/17/update-v2.2.1/">xmake v2.2.1 大版本发布，Qt, WDK和Cuda编译环境支持</a></p>
        
    </div>
</div>


        <h2 id="comments">评论</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,       
  language: 'zh-CN',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">

            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    内容
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#english">English</a></li>
                    <li><a href="#similar_posts">相关文章</a></li>
                    <li><a href="#comments">评论</a></li>
                </ul>
            </div>


            <!-- baidu ads -->
            
            
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    链接
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- xmake courses -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    xmake 入门课程
                </div>
                <a href="https://xmake.io/#/zh-cn/about/course" target="_blank">
                <img src="/static/img/xmake-course.png" alt="course" width="256" height="193">
                </a>
            </div>

            <!-- qqgroup -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    技术交流群（QQ）
                </div>
                <img src="/static/img/qqgroup.png" alt="qqgroup" width="256" height="284">
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
